package com.gonsin.gview.example.test;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.net.URL;

public class Dom4jUtils {

    public static Document parse(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;
    }

    public static int tryToParseInt(String i){
        try {
            return Integer.valueOf(i);
        } catch (Exception ex) {
            return 0;
        }
    }

    public static int tryToParseColor(String i){
        try {
            return Integer.parseInt(i.substring(1), 16);
        } catch (Exception ex) {
            return 0x00FFFFFF;
        }
    }

    public static float tryToParseFloat(String i){
        try {
            return Float.valueOf(i);
        } catch (Exception ex) {
            return 0;
        }
    }

    public static boolean tryToParseBoolean(String i){
        try {
            return i.equals("true");
        } catch (Exception ex) {
            return false;
        }
    }

}
